<template>
  <view class="cart-container" v-if="cart.length !== 0">
    <!-- 地址栏组件 -->
    <view class="address-component">
      <my-address></my-address>
    </view>
    <!-- 购物车标题 -->
    <view class="cart-title">
      <uni-icons type="shop" size="18"></uni-icons>
      <text>购物车</text>
    </view>
    <!-- 购物车商品列表 -->
    <uni-swipe-action ref="swipeAction">
      <block v-for="(goods,i) in cart" :key="goods.goods_id">
        <uni-swipe-action-item  :right-options="options" @click="swipeActionClickHandler(goods)">
             <my-goods :showRadio="true" :showNumberBox="true" :goods="goods" @radio-change="radioChangeHandler" @change-count="countChangeHandler"></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    
    <!-- 购物车结算区域 -->
    <my-settle></my-settle>
  </view>
  <!-- 空白购物车区域 -->
  <view class="empty-cart" v-else>
    <image class="empty-img" src="/static/cart_empty@2x.png"></image>
    <text class="tip-text" >空空如也</text>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  import {mapState} from 'vuex'
  export default {
    // 通过注入的方式修改tabbar购物车的徽标
    mixins: [badgeMix],
    data() {
      return {
        // 给滑动按钮添加属性
        options:[
          {
            text: '删除', // 按钮的显示文本
            style: {
              backgroundColor: '#C00000' //按钮背景颜色
            }
          }
        ]
      };
    },
    computed: {
      ...mapState('cart', ['cart'])
    },
    methods:{
      // 点击单选框的回调函数
      radioChangeHandler(e){
        // console.log(e);
        this.$store.commit('cart/changeState', e)
      },
      // 点击数量按钮的回调函数
      countChangeHandler(e){
        // console.log(e);
        this.$store.commit('cart/updateCount', e)
      },
      
      swipeActionClickHandler(goods){
        this.$store.commit('cart/delGoods', goods)
      }
    }
  }
</script>

<style lang="scss">
.cart-container{
  background-color: #fff;
  padding-bottom: 50px;
  .cart-title{
    display: flex;
    align-items: center;
    padding-left: 5px;
    height: 40px;
    font-size: 14px;
    uni-icons{
      margin-right: 5px;
    }
  }
}
.empty-cart{
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;
  .empty-img{
    width: 90px;
    height: 90px;
  }
  .tip-text{
    font-size: 12px;
    color: gray;
    margin-top: 15px;
  }
}
</style>
